home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / ddj0897.zip / DYN401.ZIP / threads / semaphor.c < prev    next >
C/C++ Source or Header  |  1997-04-16  |  6KB  |  259 lines

  1.  
  2.  
  3. /*  Copyright (c) 1993-1996 Algorithms Corporation  */
  4. /*  All rights reserved.  */
  5.  
  6.  
  7.  
  8.  
  9. /*  This file automatically generated by dpp - do not edit  */
  10.  
  11. #define    DPP_STRATEGY    2
  12. #define    DPP_FASTWIDE    0
  13.  
  14.  
  15.  
  16. #line 18 "semaphor.d"
  17. #include <string.h> 
  18.  
  19. #define    CLASS    Semaphore_c
  20. #define    ivType    Semaphore_iv_t
  21.  
  22. #include "generics.h"
  23.  
  24. object    Semaphore_c;
  25.  
  26.  
  27. #line 28 "semaphor.c"
  28. typedef struct  _Semaphore_iv_t  {
  29.     object iObj;
  30.     char * iName;
  31.     int iCount;
  32.     int iMaximum;
  33.     object iWaiting_threads;
  34.     struct _Semaphore_iv_t * iNext;
  35. }    Semaphore_iv_t;
  36.  
  37.  
  38. #line 39 "semaphor.c"
  39. typedef struct  _Semaphore_cv_t  {
  40.     struct _Semaphore_iv_t * cMsl;
  41. }    Semaphore_cv_t;
  42.  
  43. static    Semaphore_cv_t    *Semaphore_cv;
  44.  
  45.  
  46.  
  47. #line 34 "semaphor.d"
  48. cmeth objrtn New(object self, char *name, int cnt, int mx)
  49.     object obj = oSuper(Semaphore_c, gNew, self)(self); 
  50.     ivType *iv = ivPtr(obj); 
  51.  
  52.     iv->iObj = obj; 
  53.     if (name) { 
  54.         iv->iName = Tnalloc(char, strlen(name)+1); 
  55.         strcpy(iv->iName, name); 
  56.     } 
  57.     iv->iCount = cnt; 
  58.     iv->iMaximum = mx; 
  59.     iv->iNext = Semaphore_cv->cMsl; 
  60.     Semaphore_cv->cMsl = iv; 
  61.     return obj; 
  62.  
  63. cmeth objrtn Semaphore_cm_gNew(object self)
  64.     return New(self, NULL, 1, 1); 
  65.  
  66. imeth int Semaphore_im_gWaitFor(object self)
  67. { Semaphore_iv_t *iv = GetIVs(Semaphore, self);
  68.     object ct; 
  69.  
  70.     INHIBIT_THREADER; 
  71.     if (!(ct = gFindStr(Thread, NULL))) { 
  72.         ENABLE_THREADER; 
  73.         return 0; 
  74.     } 
  75.     if (iv->iCount) { 
  76.         iv->iCount--; 
  77.         ENABLE_THREADER; 
  78.         return 0; 
  79.     } 
  80.     gWaitSemaphore(ct, self); 
  81.  
  82.     if (!iv->iWaiting_threads) 
  83.         iv->iWaiting_threads = gNew(LinkObject); 
  84.     gAddLast(iv->iWaiting_threads, ct); 
  85.     ENABLE_THREADER; 
  86.  
  87.     __dynace_yield(); 
  88.     return 0; 
  89.  
  90. imeth objrtn Release(object self, int cnt)
  91. { Semaphore_iv_t *iv = GetIVs(Semaphore, self);
  92.     object thread; 
  93.  
  94.     iv->iCount += cnt; 
  95.     if (iv->iCount > iv->iMaximum) 
  96.         iv->iCount = iv->iMaximum; 
  97.     INHIBIT_THREADER; 
  98.     while (iv->iCount && iv->iWaiting_threads && (thread = gFirst(iv->iWaiting_threads))) { 
  99.         gDisposeFirst(iv->iWaiting_threads); 
  100.         gReleaseSemaphore(thread); 
  101.         iv->iCount--; 
  102.     } 
  103.     ENABLE_THREADER; 
  104.     return self; 
  105.  
  106. imeth objrtn Semaphore_im_gDispose(object self)
  107. { Semaphore_iv_t *iv = GetIVs(Semaphore, self);
  108.     ivType *t, *p; 
  109.  
  110.     INHIBIT_THREADER; 
  111.     while (iv->iWaiting_threads && gFirst(iv->iWaiting_threads)) 
  112.         Release(self, iv->iMaximum); 
  113.     if (iv->iWaiting_threads) 
  114.         gDispose(iv->iWaiting_threads); 
  115.     if (iv->iName) 
  116.         free(iv->iName); 
  117.     for (p=NULL, t=Semaphore_cv->cMsl ; t ; p=t, t=t->iNext) 
  118.         if (t == iv) { 
  119.         if (p) 
  120.             p->iNext = t->iNext; 
  121.         else 
  122.             Semaphore_cv->cMsl = t->iNext; 
  123.         break; 
  124.     } 
  125.     oSuper(Semaphore_c, gDispose, self)(self); 
  126.     ENABLE_THREADER; 
  127.     return NULL; 
  128.  
  129. imeth objrtn Semaphore_im_gGCDispose(object self)
  130. { Semaphore_iv_t *iv = GetIVs(Semaphore, self);
  131.     ivType *t, *p; 
  132.  
  133.     INHIBIT_THREADER; 
  134.     if (iv->iWaiting_threads && gFirst(iv->iWaiting_threads)) { 
  135.         ENABLE_THREADER; 
  136.         return NULL; 
  137.     } 
  138.     if (iv->iName) 
  139.         free(iv->iName); 
  140.     for (p=NULL, t=Semaphore_cv->cMsl ; t ; p=t, t=t->iNext) 
  141.         if (t == iv) { 
  142.         if (p) 
  143.             p->iNext = t->iNext; 
  144.         else 
  145.             Semaphore_cv->cMsl = t->iNext; 
  146.         break; 
  147.     } 
  148.     oSuper(Semaphore_c, gDispose, self)(self); 
  149.     ENABLE_THREADER; 
  150.     return NULL; 
  151.  
  152. cmeth objrtn Semaphore_cm_gFindStr(object self, char *name)
  153.     ivType *iv; 
  154.  
  155.     USE(self); 
  156.     if (!name) 
  157.         return NULL; 
  158.     for (iv=Semaphore_cv->cMsl ; iv ; iv=iv->iNext) 
  159.         if (iv->iName && !strcmp(iv->iName, name)) 
  160.         return iv->iObj; 
  161.     return NULL; 
  162.  
  163. imeth int Semaphore_im_gCount(object self)
  164. { Semaphore_iv_t *iv = GetIVs(Semaphore, self);
  165.     return iv->iCount; 
  166.  
  167. imeth char * Semaphore_im_gName(object self)
  168. { Semaphore_iv_t *iv = GetIVs(Semaphore, self);
  169.     return iv->iName; 
  170.  
  171.  
  172.  
  173. imeth objrtn Semaphore_im_gRemoveWaits(object self, object thrd)
  174. { Semaphore_iv_t *iv = GetIVs(Semaphore, self);
  175.     object linkSequence, linkValue, thread; 
  176.  
  177.     if (!iv->iWaiting_threads) 
  178.         return self; 
  179.     INHIBIT_THREADER; 
  180.     linkSequence = gSequenceLinks(iv->iWaiting_threads); 
  181.     while (linkValue = gNext(linkSequence)) { 
  182.         thread = gValue(linkValue); 
  183.         if (thread == thrd) { 
  184.             gDispose(linkValue); 
  185.             break; 
  186.         } 
  187.     } 
  188.     if (linkValue) 
  189.         gDispose(linkSequence); 
  190.     ENABLE_THREADER; 
  191.     return self; 
  192.  
  193. imeth objrtn Semaphore_im_gCopy(object self)
  194.     return gShouldNotImplement(self, "Copy/DeepCopy"); 
  195.  
  196.  
  197. #line 213 "semaphor.c"
  198.  
  199. objrtn    Semaphore_initialize(void)
  200. {
  201.     static  CRITICALSECTION  cs;
  202.     static  int volatile once = 0;
  203.  
  204.     ENTERCRITICALSECTION(_CI_CS_);
  205.     if (!once) {
  206.         INITIALIZECRITICALSECTION(cs);
  207.         once = 1;
  208.     }
  209.     LEAVECRITICALSECTION(_CI_CS_);
  210.  
  211.     ENTERCRITICALSECTION(cs);
  212.  
  213.     if (Semaphore_c) {
  214.         LEAVECRITICALSECTION(cs);
  215.         return Semaphore_c;
  216.     }
  217.     INHIBIT_THREADER;
  218.     Semaphore_c = gNewClass(Class, "Semaphore", sizeof(Semaphore_iv_t), sizeof(Semaphore_cv_t), END);
  219.     cMethodFor(Semaphore, gNewSemaphore, New);
  220.     cMethodFor(Semaphore, gFindStr, Semaphore_cm_gFindStr);
  221.     cMethodFor(Semaphore, gNew, Semaphore_cm_gNew);
  222.     iMethodFor(Semaphore, gCount, Semaphore_im_gCount);
  223.     iMethodFor(Semaphore, gRemoveWaits, Semaphore_im_gRemoveWaits);
  224.     iMethodFor(Semaphore, gRelease, Release);
  225.     iMethodFor(Semaphore, gDispose, Semaphore_im_gDispose);
  226.     iMethodFor(Semaphore, gName, Semaphore_im_gName);
  227.     iMethodFor(Semaphore, gGCDispose, Semaphore_im_gGCDispose);
  228.     iMethodFor(Semaphore, gCopy, Semaphore_im_gCopy);
  229.     iMethodFor(Semaphore, gDeepDispose, Semaphore_im_gDispose);
  230.     iMethodFor(Semaphore, gDeepCopy, Semaphore_im_gCopy);
  231.     iMethodFor(Semaphore, gWaitFor, Semaphore_im_gWaitFor);
  232.  
  233.     Semaphore_cv = GetCVs(Semaphore);
  234.  
  235.     ENABLE_THREADER;
  236.  
  237.     LEAVECRITICALSECTION(cs);
  238.  
  239.     return Semaphore_c;
  240. }
  241.  
  242.  
  243.  
  244.